
/* GCSx
** SCRIPTPROP.CPP
**
** Script properties dialog
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#include "all.h"

// Properties dialog

ScriptPropertiesDialog* ScriptPropertiesDialog::dialogScript = NULL;
ScriptPropertiesDialog* ScriptPropertiesDialog::dialogLibrary = NULL;
ScriptPropertiesDialog* ScriptPropertiesDialog::dialogNotes = NULL;

ScriptPropertiesDialog* ScriptPropertiesDialog::createScript() { start_func
    if (!dialogScript) {
        dialogScript = new ScriptPropertiesDialog(Script::SCRIPT_CODE);
    }
    return dialogScript;
}
ScriptPropertiesDialog* ScriptPropertiesDialog::createLibrary() { start_func
    if (!dialogLibrary) {
        dialogLibrary = new ScriptPropertiesDialog(Script::SCRIPT_LIBRARY);
    }
    return dialogLibrary;
}
ScriptPropertiesDialog* ScriptPropertiesDialog::createNotes() { start_func
    if (!dialogNotes) {
        dialogNotes = new ScriptPropertiesDialog(Script::SCRIPT_NOTE);
    }
    return dialogNotes;
}

void ScriptPropertiesDialog::destroy() { start_func
    if (dialogScript) {
        delete dialogScript;
        dialogScript = NULL;
    }
    if (dialogLibrary) {
        delete dialogLibrary;
        dialogLibrary = NULL;
    }
    if (dialogNotes) {
        delete dialogNotes;
        dialogNotes = NULL;
    }
}

ScriptPropertiesDialog::ScriptPropertiesDialog(int type) : ImageDialog() { start_func
    initialized = 0;
    forType = type;
}

ScriptPropertiesDialog::~ScriptPropertiesDialog() { start_func
}

Dialog::ButtonAction ScriptPropertiesDialog::verifyEntry(int buttonId, Dialog::ButtonAction buttonType) { start_func
    if ((buttonType != BUTTON_APPLY) && (buttonType != BUTTON_OK)) return BUTTON_DEFAULT;

    // Another script/notes with same name?
    string lName = dynamic_cast<WTextBox*>(findWidget(ID_NAME))->state();
    toLower(lName);

    if (forType == Script::SCRIPT_CODE) {
        const Script* found = script->getWorld()->findScriptCode(lName);
        if ((found) && (found != script)) {
            guiErrorBox("A script by that name already exists- please choose another.", errorTitleDuplicateName);
            return BUTTON_NOTHING;
        }
        if (lName.empty()) {
            guiErrorBox("Please enter a name for this script.", errorTitleMissingName);
            return BUTTON_NOTHING;
        }
    }

    if (forType == Script::SCRIPT_NOTE) {
        const Script* found = script->getWorld()->findScriptNote(lName);
        if ((found) && (found != script)) {
            guiErrorBox("Notes by that name already exist- please choose another.", errorTitleDuplicateName);
            return BUTTON_NOTHING;
        }
        if (lName.empty()) {
            guiErrorBox("Please enter a name for these notes.", errorTitleMissingName);
            return BUTTON_NOTHING;
        }
    }

    if (forType == Script::SCRIPT_LIBRARY) {
        const Script* found = script->getWorld()->findScriptLib(lName);
        if ((found) && (found != script)) {
            guiErrorBox("A function library by that name already exists- please choose another.", errorTitleDuplicateName);
            return BUTTON_NOTHING;
        }
        if (lName.empty()) {
            guiErrorBox("Please enter a name for this function library.", errorTitleMissingName);
            return BUTTON_NOTHING;
        }
    }

    return Dialog::verifyEntry(buttonId, buttonType);
}

int ScriptPropertiesDialog::run(string* title, AnimGroup** animGroup, TileSet** tileSet, int* animTileId, const ScriptEdit* whichScript, WorldEdit* whichWorld) { start_func
    assert(title);
    if (forType == Script::SCRIPT_CODE) {
        assert(animGroup);
        assert(tileSet);
        assert(animTileId);
    }

    script = whichScript;

    // @TODO: Tooltips on this dialog
    if (!initialized) {
        Widget* w = NULL;

        w = new WStatic(ID_LABEL, "\tName:");
        w->addTo(this);

        w = new WTextBox(ID_NAME, title, 0);
        w->addTo(this);

        if (forType == Script::SCRIPT_CODE) {
            w = new WStatic(ID_LABEL, "\tDefault sprite:");
            w->addTo(this);
            
            addLibTree(ID_LIBRARY);
    
            w = new WStatic(ID_LABEL, "\tImage:");
            w->addTo(this);
            
            addImgChoose(ID_IMAGE);
        }
        
        w = new WButton(ID_OK, messageBoxOK, BUTTON_OK);
        w->addTo(this);

        w = new WButton(ID_CANCEL, messageBoxCancel, BUTTON_CANCEL);
        w->addTo(this);

        makePretty(2, 0);

        initialized = 1;
    }
    else {
        findWidget(ID_NAME)->changeStorage(title);
    }
    
    if (forType == Script::SCRIPT_CODE) {
        runPrep(whichWorld, *animGroup, *tileSet, *animTileId);
        setTitle("Script Properties");
    }
    else if (forType == Script::SCRIPT_LIBRARY) {
        setTitle("Function Library Properties");
    }
    else {
        setTitle("Notes Properties");
    }

    // Hit OK?
    if (runModal() == ID_OK) {
        if (forType == Script::SCRIPT_CODE) {
            runComplete(animGroup, tileSet, animTileId);
        }
        return 1;
    }
    
    if (forType == Script::SCRIPT_CODE) runComplete();
    return 0;
}

